<?php

namespace App\Http\Controllers\OpenShop\SiteLocation;


use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Http\Controllers\BaseController;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\SiteLocation\SiteLocationInputService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SiteEvaluateInputController extends BaseController
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function info(Request $request): JsonResponse
    {
        // 房源orderNo
        $orderNo = (string)$request->get('orderNo');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'orderNo' => 'required|string',
        ]);

        $data = (new SiteLocationInputService($orderNo, $userId))->getInputObjectFill();

        return $this->success($data);
    }

    /**
     * @throws NotFoundExceptionInterface
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws ValidationException
     */
    public function update(Request $request): JsonResponse
    {
        // 房源orderNo
        $orderNo = (string)$request->get('orderNo');
        $field   = (string)$request->get('field');
        $value   = $request->get('value');
        $this->validate($request, [
            'orderNo' => 'required|string',
            'field'   => 'required|string',
        ]);

        $userId = getRmsUserId();

        (new SiteLocationInputService($orderNo, $userId))->saveUserContentJson($field, $value);

        return $this->success();
    }

    /**
     * 房源自评提交
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function submit(Request $request): JsonResponse
    {
        // 房源orderNo
        $orderNo = (string)$request->get('orderNo');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'orderNo' => 'required|string',
        ]);

        (new SiteLocationInputService($orderNo, $userId))->submitOrder();

        return $this->success();
    }

    public function auditForm(Request $request)
    {
        // 房源orderNo
        $orderNo = (string)$request->get('orderNo');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'orderNo' => 'required|string',
        ]);

        $data = (new SiteLocationInputService($orderNo, $userId))->auditForm();

        return $this->success($data);
    }

    public function audit(Request $request)
    {
        // 房源orderNo
        $orderNo = (string)$request->get('orderNo');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'orderNo'      => 'required|string',
            'scenicScore'  => 'required|integer|min:0|max:40',
            'streetScore'  => 'required|integer|min:0|max:30',
            'shopScore'    => 'required|integer|min:0|max:20',
            'takeoutScore' => 'required|integer|min:0|max:10',
            'rentGrade'    => ['required', 'integer', Rule::in(array_keys(SiteLocationEnum::COST_PERFORMANCES))],
            'content'      => ['required', 'string', 'min:1', 'max:1000'],
        ]);

        (new SiteLocationInputService($orderNo, $userId))->audit($request->all());

        return $this->success();
    }

    public function result(Request $request)
    {
        $orderNo = (string)$request->get('orderNo');
        $userId  = getRmsUserId();

        $this->validate($request, [
            'orderNo' => 'required|string',
        ]);

        return $this->success((new SiteLocationInputService($orderNo, $userId))->result());
    }

    public function reportUpdate(Request $request)
    {

        $orderNo = $request->post('orderNo', '');

        $userId    = getRmsUserId();

        $res = (new SiteLocationInputService($orderNo, $userId))->reportUpdate($request->all());

        return $res ? $this->success() : $this->fail(403, '您没有对应修改的权限');
    }
}
